package mianshi;

import java.util.HashMap;
import java.util.Map;
// 单例模式的本质就是控制实例的数目
public class OneExtend {
	private static final String DEFAULT_PREKEY = "Cache";
	private static Map<String, OneExtend> map = new HashMap<String, OneExtend>();
	// 用来记录正在使用第几个实例，到了控制的最大数目，就返回从1开始
	private static int num = 1;
	private static final int NUM_MAX = 3;
	private OneExtend() {
	}
	
	public static OneExtend getInstanc() {
		String key = DEFAULT_PREKEY + num;
	    OneExtend oneExtend = map.get(key);
	    if (oneExtend == null) {
			oneExtend = new OneExtend();
			map.put(key, oneExtend);
		}
	    
	    num++;
	    if (num > NUM_MAX) {
			num = 1;
		}
	    
	    return oneExtend;
	}
	
	public static void main(String[] args) {
		OneExtend instanc1 = getInstanc();
		OneExtend instanc2 = getInstanc();
		OneExtend instanc3 = getInstanc();
		OneExtend instanc4 = getInstanc();
		OneExtend instanc5 = getInstanc();
		OneExtend instanc6 = getInstanc();
		System.out.println(instanc1);
		System.out.println(instanc2);
		System.out.println(instanc3);
		System.out.println(instanc4);
		System.out.println(instanc5);
		System.out.println(instanc6);
	}
	
}
